const {
  releaseAccommodationService,
  getAccommodationService,
  getAccommodationByHotService,
  likeAccommodationService,
  saveAccommodationService,
  getSaveAccommodationService,
  reservationService,
  getReservationService,
} = require("../service/accommodation.service");

class accommodationController {
  // 发布
  async releaseAccommodation(ctx, next) {
    const parameter = ctx.parameterData;
    parameter.view_counts = Math.floor(Math.random() * 1000) + 500;
    // console.log("appointmentStudyRoom", parameter);

    const result = await releaseAccommodationService(parameter);

    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `发布住宿成功~`,
      data: result,
    };
  }

  // 获取
  async getAccommodation(ctx, next) {
    const parameter = ctx.parameterData;
    console.log("getAccommodation", parameter);
    const result = await getAccommodationService(parameter);
    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取住宿成功~`,
      data: result,
    };
  }

  async getAccommodationByHot(ctx, next) {
    const parameter = ctx.parameterData;
    console.log("getAccommodation", parameter);
    const result = await getAccommodationByHotService(parameter);
    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `按照热度获取住宿成功~`,
      data: result,
    };
  }

  // 模糊查询
  async likeAccommodation(ctx, next) {
    const { keyWord } = ctx.params;
    const { limit, offset } = ctx.parameterData;
    // console.log(keyWord);
    const result = await likeAccommodationService(keyWord, limit, offset);
    // console.log("controller", result);
    ctx.body = {
      code: 200,
      message: `搜索数据成功~`,
      data: result,
    };
  }

  saveAccommodation = (option) => {
    return async (ctx, next) => {
      const parameter = ctx.parameterData;
      const result = await saveAccommodationService(parameter, option.method);

      //4.查看存储的结果，·告知前端创建成
      ctx.body = {
        code: 200,
        message: `${option.type}成功~`,
        data: result,
      };
    };
  };

  // 获取
  async getSaveAccommodation(ctx, next) {
    const parameter = ctx.parameterData;
    console.log(parameter);
    const result = await getSaveAccommodationService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取所有收藏成功~`,
      data: result,
    };
  }

  // 预约
  reservation = (option) => {
    return async (ctx, next) => {
      const parameter = ctx.parameterData;
      const result = await reservationService(parameter, option.method);

      //4.查看存储的结果，·告知前端创建成
      ctx.body = {
        code: 200,
        message: `${option.type}成功~`,
        data: result,
      };
    };
  };

  // 获取
  async getReservation(ctx, next) {
    const parameter = ctx.parameterData;
    console.log(parameter);
    const result = await getReservationService(parameter);

    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取所有下单成功~`,
      data: result,
    };
  }
}

module.exports = new accommodationController();
